package com.qztc.controller;

import com.qztc.hire_end_dto.dto.WelfareType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static com.qztc.config.GlobalConfig.SERVICE_URL;

@RestController
@RequestMapping("/consumer/welfareType")
@Tag(name = "消费者端福利类型管理接口")
public class WelfareTypeControllerByRestTemplate {
    @Autowired
    private RestTemplate restTemplate;

    @Operation(summary = "查询所有福利类型")
    @GetMapping("/getAll")
    public String getAll() {
        return restTemplate.getForObject(SERVICE_URL+"/welfareType/getAll", String.class);
    }

    @Operation(summary = "查询福利类型")
    @GetMapping("/getbywelfareTypeId")
    public String getbywelfareTypeId(@RequestParam Integer welfareTypeId) {
        return restTemplate.getForObject(SERVICE_URL+"/welfareType/getbywelfareTypeId?welfareTypeId="+welfareTypeId, String.class);
    }

    @Operation(summary = "修改福利类型")
    @PutMapping("/update")
    public String update(@RequestBody WelfareType welfareType){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<WelfareType> entity = new HttpEntity<>(welfareType,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/welfareType/update", HttpMethod.PUT,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "删除福利类型")
    @DeleteMapping("/delete")
    public String delete(@RequestBody WelfareType welfareType){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<WelfareType> entity = new HttpEntity<>(welfareType,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/welfareType/delete",HttpMethod.DELETE,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "增加福利类型")
    @PostMapping("/add")
    public String add(@RequestBody WelfareType welfareType){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<WelfareType> entity = new HttpEntity<>(welfareType,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/welfareType/add",HttpMethod.POST,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "批量删除福利类型")
    @DeleteMapping("/deleteMany")
    public String deleteMany(@RequestBody List<Integer> welfareTypeIds){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Integer>> entity = new HttpEntity<>(welfareTypeIds,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/welfareType/deleteMany",HttpMethod.DELETE,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "批量增加福利类型")
    @PostMapping("/addMany")
    public String addMany(@RequestBody List<WelfareType> welfareTypes){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<WelfareType>> entity = new HttpEntity<>(welfareTypes,headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/welfareType/addMany",HttpMethod.POST,entity, String.class);
        return result.getBody();
    }
}
